package com.jiyi.activity.iot;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jiyi.adapter.DividerItemDecoration;
import com.jiyi.adapter.recyclerview.CommonAdapter;
import com.jiyi.adapter.recyclerview.base.ViewHolder;
import com.jiyi.adapter.recyclerview.wrapper.EmptyWrapper;
import com.jiyi.bean.iot.FireAlarmBean;
import com.jiyi.exception.SampleRefreshObserver;
import com.jiyi.inteface.OnRcvScrollListener;
import com.jiyi.retrofit.IotHttpMethods;
import com.swift.base.ActivityBase;
import com.swift.maintenance.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/4/26.
 */

public class FireAlarmActivity extends ActivityBase implements SwipeRefreshLayout.OnRefreshListener {
    @BindView(R.id.tvBack)
    TextView tvBack;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvSearch)
    TextView tvSearch;
    @BindView(R.id.ivHint)
    ImageView ivHint;
    @BindView(R.id.top_layout)
    RelativeLayout topLayout;
    @BindView(R.id.mainList)
    RecyclerView mainList;
    @BindView(R.id.refreshLayout)
    SwipeRefreshLayout refreshLayout;
    @BindView(R.id.root)
    LinearLayout root;

    private List<FireAlarmBean> mDatas = new ArrayList<FireAlarmBean>();
    EmptyWrapper adapter;
    ViewGroup container;
    int type;
    String typeName;
    Dialog dialog;

    // 是否刷新模式
    boolean isRefresh = false;
    int pageNo = 1;// 页码
    private final int LOAD_DATA = 8; // 加载数据事件
    private boolean loadfinish = true; // 指示数据是否加载完成

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fire_alarm);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        getIntentData();
        setTop();
        onRefresh();
        setAdapter();
    }

    private void getIntentData() {
        type = getIntent().getIntExtra("type",1);
        switch (type)
        {
            case 1:
                typeName = "火警";
                break;
            case 2:
                typeName = "故障";
                break;
            case 3:
                typeName = "测试";
                break;
        }
    }

    private void setTop() {
        tvBack.setText("");
        tvTitle.setText(typeName+"实时监测");
        tvSearch.setVisibility(View.INVISIBLE);
    }

    @Override
    public void onRefresh() {
        isRefresh = true;
        pageNo = 1;
        mHandler.sendEmptyMessage(LOAD_DATA);
    }

    private void setAdapter() {


        // 设置refreshLayout的监听
        refreshLayout.setColorSchemeColors(Color.RED, Color.YELLOW, Color.BLUE,
                Color.GREEN);
        refreshLayout.setOnRefreshListener(this);

        mainList.setLayoutManager(new LinearLayoutManager(this));
        mainList.addItemDecoration(new DividerItemDecoration(this,
                DividerItemDecoration.VERTICAL_LIST, null));

        CommonAdapter<FireAlarmBean> commonAdapter = new CommonAdapter<FireAlarmBean>(this, R.layout.item_fire_alarm, mDatas) {
            @Override
            protected void convert(final ViewHolder holder,final FireAlarmBean o, int position) {

                switch (type)
                {
                    case 1:
                        holder.setImageResource(R.id.ivCat,R.mipmap.icon_fire_item);
                        break;
                    case 2:
                        holder.setImageResource(R.id.ivCat,R.mipmap.icon_guzhang_item);
                        break;
                    case 3:
                        holder.setImageResource(R.id.ivCat,R.mipmap.icon_test_item);
                        break;
                        default:
                            holder.setImageResource(R.id.ivCat,R.mipmap.icon_fire_item);
                            break;
                }
                holder.setText(R.id.tvUnitName, TextUtils.isEmpty(o.getMonitorname())? o.getShortname():o.getMonitorname());
                holder.setText(R.id.tvSummary,o.getAlarm_summary());
                holder.setText(R.id.tvHappenTime,o.getHappen_time());
                holder.setText(R.id.tvReceiveTime,o.getReceive_time());

                holder.setOnClickListener(R.id.llRoot, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(v.getContext(),FireAlarmDetailActivity.class);
                        intent.putExtra("sn",o.getAlarm_sn());
                        intent.putExtra("type",type);
                        intent.putExtra("typeName",typeName);
                        startActivity(intent);
                    }
                });
            }


        };

        adapter = new EmptyWrapper(commonAdapter);

        View emptyView = LayoutInflater.from(this).inflate(R.layout.empty_view_text_and_image,
                container, false);
        ImageView ivEmptyTip = (ImageView) emptyView
                .findViewById(R.id.ivEmptyTip);
        TextView tvEmptyTip = (TextView) emptyView
                .findViewById(R.id.tvEmptyTip);
        ivEmptyTip.setImageResource(R.mipmap.tip_empty_huodong);
        tvEmptyTip.setText(R.string.NO_DATA);
        adapter.setEmptyView(emptyView);

        mainList.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        mainList.addOnScrollListener(new OnRcvScrollListener() {
            @Override
            public void onBottom() {
                super.onBottom();
                if (loadfinish) {
                    Log.e("加载到底部 pageNo = ", pageNo + "");
                    loadMore();
                }

            }
        });
    }

    /**
     * 处理中心
     */
    Handler mHandler = new Handler() {
        public void dispatchMessage(android.os.Message msg) {
            switch (msg.what) {
                case LOAD_DATA:
                    if (loadfinish) {
                        loadfinish = false;
                        getData();
                    }
                    break;
                default:
                    break;
            }
        }

        ;
    };

    /**
     * 作者：hym 日期：2017-4-6 描述：加载更多数据
     */
    void loadMore() {
        isRefresh = false;
        mHandler.sendEmptyMessage(LOAD_DATA);
    }

    private void getData() {
        IotHttpMethods.getInstance().getFireAlarmList(type)
                .subscribe(new SampleRefreshObserver<List<FireAlarmBean>>(this, refreshLayout) {
                    @Override
                    public void onNext(List<FireAlarmBean> tasks) {
                        if (isRefresh) {
                            mDatas.clear();
                        }

                        if (tasks.size() > 0) {
                            mDatas.addAll(tasks);
                            pageNo++;
                        }

                        adapter.notifyDataSetChanged();
                        loadfinish = true; // 加载完成
                    }

                });
    }

    @OnClick({R.id.tvBack})
    public void onClick(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.tvBack:
                finish();
                break;

        }
    }
}
